package org.czl.tree;

import java.util.Arrays;

public class s {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(findXSum(new int[]{1, 1, 2, 2, 3, 4, 2, 3}, 6, 2)));
    }
    public static int[] findXSum(int[] nums, int k, int x) {
        int n = nums.length;
        int[] ans = new int[n - k + 1];

        for(int i = 0; i + k - 1 < n; i++){
            int[][] map = new int[50][2];

            for(int j = i; j <= i + k - 1;j++){
                map[nums[j]][0] = nums[j];
                map[nums[j]][1]++;
            }
            Arrays.sort(map,(a,b) -> a[1] != b[1] ?b[1] - a[1] : b[0] - a[0]);
            int ret = 0;
            for(int c = 0; c < x; c++){
                if(map[c][1] != 0){
                    ret += map[c][0] * map[c][1];
                }else{
                    break;
                }
            }
            ans[i] = ret;

        }
        return ans;
    }
}
